package com.hczx.caxa.service;

import com.google.common.collect.Maps;
import com.hczx.caxa.util.HttpUtil;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

@Service
public class CaxaApiService {

    private static Logger logger = LoggerFactory.getLogger(CaxaApiService.class);

    @Value("${informat-api.updateCxpFile}")
    private String updateCxpFile;

    private final MinioService minioService;
    private final LogFileService logFile;

    public CaxaApiService(MinioService minioService, LogFileService logFile) {
        this.minioService = minioService;
        this.logFile = logFile;
    }

    public String updateCxpFile(MultipartFile pdfFile, MultipartFile cxpFile) {

        //String pdfFileName = "/caxa/" + pdfFile.getOriginalFilename();
        String cxpFileName = "/caxa/" + cxpFile.getOriginalFilename();
        
        //String pdfPath = minioService.upload(pdfFile, pdfFileName, "application/pdf");
        String cxpPath = minioService.upload(cxpFile, cxpFileName, "application/octet-stream");
        
        String message = updatePdfAndXML(cxpFile);
        String pdfPath = "";
        String xmlPath = "";
        if(!StringUtils.equals(message, "failure")) {
        	String[] split = message.split("\\|");
        	pdfPath = split[0];
        	xmlPath = split[1];
        }

        Map<String, String> paramMap = Maps.newHashMap();
        paramMap.put("cxpFileName", cxpFile.getOriginalFilename());
        paramMap.put("cxpFileUrl", cxpPath);
        paramMap.put("pdfFileName", cxpFile.getOriginalFilename().replace(".cxp", ".pdf"));
        paramMap.put("pdfFileUrl", pdfPath);
        paramMap.put("xmlFileName", cxpFile.getOriginalFilename().replace(".cxp", ".xml"));
        paramMap.put("xmlFileUrl", xmlPath);

        return HttpUtil.getPostResponse(updateCxpFile, paramMap);
    }

    private String updatePdfAndXML(MultipartFile cxpFile) {
		String endpoint = "http://172.26.100.8:9100/cas-services/caxa/convertFile";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", cxpFile.getResource());

        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.exchange(endpoint, HttpMethod.POST, requestEntity, String.class);
        int statusCode = response.getStatusCodeValue(); // 获取响应状态码
        String responseBody = response.getBody(); // 获取响应体
        
        logger.info("responseBody:{}", statusCode+"::"+responseBody);
        
        return responseBody;
    }
    
}
